package com.ruoyi.web.controller.yada;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.WeatherUtils;
import com.ruoyi.system.domain_yada.*;
import com.ruoyi.system.service_yada.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.jfr.Frequency;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.spring.web.json.Json;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.http.HttpRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/Special")
@Api(tags = "模板导入")
public class SpecialController {

    @Resource
    private IRegionVGIService regionVGIService;

    @Resource
    private IAustraliaMiddleEastService australiaMiddleEastService;

    @Resource
    private IAorestCoverageService service;

    @Resource
    private IVegetationCoverageService coverageService;

    @Resource
    private IRegionalSystemService systemService;

    /**
     */

    @RequestMapping(value = "/helpSaveExcel")
    private AjaxResult helpSaveExcel(@RequestBody MultipartFile file) {
        List<RegionVGIVO> value = saveExcel(file);

        return IntoRegion(value);
    }

    @RequestMapping(value = "/helpWeatherClass",method = {RequestMethod.GET})
    public  AjaxResult helpWeatherClass(String city,String type)
    {
        String url="https://restapi.amap.com/v3/weather/weatherInfo";
        Map<String,String> map=new HashMap<>();
        map.put("key","37087f0a3007dc67d37859553cd02d33");
        map.put("city",city);
        if (type!=null){ map.put("extensions",type);}
        map.put("output","json");
        String value= WeatherUtils.httpRequest(url,map);
        WeatherUtilsVO vo = JSON.parseObject(value,WeatherUtilsVO.class);
//        String value2=vo.getForecasts().substring(1,vo.getForecasts().length()-1);
//        System.out.println(value2);
//        vo.setForecast(JSON.parseObject(value2,ForecastVo.class));
//        JSONObject userJson=JSONObject.parseObject(value);
//        WeatherUtilsVO utilsValue= JSON.toJavaObject(userJson,WeatherUtilsVO.class);

        return AjaxResult.success(vo);
    }    /**
     * 森林覆盖率模板导入
     */
    @RequestMapping(value = "/helpExcelAorest")
    private AjaxResult helpExcelAorest(@RequestBody MultipartFile file) {
        List<AorestCoverageVO> value = saveExcelAorest(file);

        return IntoAorestCoverage(value);
    }

    /**
     *平均植被覆盖度和净初级生产力
     */
    @RequestMapping(value = "/helpExcelnpp")
    private AjaxResult helpExcelnpp(@RequestBody MultipartFile file) {
        List<VegetationCoverageVO> value = saveExcelnpp(file);

        return IntoVegetation(value);
    }

    /**
     * 澳大利亚中部地区模板导入
     */
    @RequestMapping(value = "/helpExcelAus")
    private AjaxResult helpExcelAus(@RequestBody MultipartFile file) {
        List<AustraliaMiddleEastVO> value = saveExcelAus(file);

        return IntoAustralia(value);
    }

    /**
     * 区域生态系统类型转移
     */
    @RequestMapping(value = "/helpExcelRegion")
    private AjaxResult helpExcelRegion(@RequestBody MultipartFile file) {
        List<RegionalSystemVO> value = saveExcelRegion(file);

        return IntoRegional(value);
    }

    public AjaxResult IntoRegional(List<RegionalSystemVO> eastVOS)
    {
        try {
            for (int i = 0; i < eastVOS.size(); i++) {
                eastVOS.get(i).setCreateTime(LocalDateTime.now());
                systemService.IntoRegional(eastVOS.get(i));
            }
            return AjaxResult.success();
        }
        catch (Exception E){
            E.getMessage();
            return  AjaxResult.error(E.getMessage());
        }
    }

    public AjaxResult IntoAustralia(List<AustraliaMiddleEastVO> eastVOS) {

        for (int i = 0; i < eastVOS.size(); i++) {
            eastVOS.get(i).setCreateTime(LocalDateTime.now());
            australiaMiddleEastService.IntoAustralia(eastVOS.get(i));
        }

        return AjaxResult.success();
    }

    public AjaxResult IntoRegion(List<RegionVGIVO> vgivo) {
        for (int i = 0; i < vgivo.size(); i++) {
            vgivo.get(i).setCreatedTime(LocalDateTime.now());

            regionVGIService.IntoRegion(vgivo.get(i));
        }

        return AjaxResult.success();

    }

    public AjaxResult IntoAorestCoverage(List<AorestCoverageVO> eastVOS)
    {
        try {
            for (int i = 0; i < eastVOS.size(); i++) {
                eastVOS.get(i).setCreateTime(LocalDateTime.now());
                service.IntoAorestCoverage(eastVOS.get(i));
            }
            return AjaxResult.success();
        }
        catch (Exception E){
            E.getMessage();
            return  AjaxResult.error(E.getMessage());
        }
    }

    public AjaxResult IntoVegetation(List<VegetationCoverageVO> eastVOS)
    {
        try {
            for (int i = 0; i < eastVOS.size(); i++) {
                eastVOS.get(i).setCreateTime(LocalDateTime.now());
                coverageService.IntoVegetation(eastVOS.get(i));
            }
            return AjaxResult.success();
        }
        catch (Exception E){
            E.getMessage();
            return  AjaxResult.error(E.getMessage());
        }
    }

    private List<RegionVGIVO> saveExcel(MultipartFile file) {

        try {
            List<RegionVGIVO> Value = new ArrayList<>();
            InputStream inputStream = file.getInputStream();
            Workbook book;
            Sheet sheet;
            book = new XSSFWorkbook(inputStream);
            sheet = book.getSheetAt(0);
            for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
                RegionVGIVO value = new RegionVGIVO();
                Row row = sheet.getRow(i);
                value.setCreatedTime(LocalDateTime.now());
                value.setRegion(row.getCell(0).toString());
                value.setRegionEn(row.getCell(1).toString());
                value.setForest(Double.parseDouble(row.getCell(2).toString()));
                value.setGrassland(Double.parseDouble(row.getCell(3).toString()));
                value.setParticularYear(row.getCell(4).toString());
                Value.add(value);
            }
            return Value;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }


    }

    private List<AustraliaMiddleEastVO> saveExcelAus(MultipartFile file) {

        try {
            List<AustraliaMiddleEastVO> Value = new ArrayList<>();
            InputStream inputStream = file.getInputStream();
            Workbook book;
            Sheet sheet;
            book = new XSSFWorkbook(inputStream);
            sheet = book.getSheetAt(0);
            for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
                AustraliaMiddleEastVO value = new AustraliaMiddleEastVO();
                Row row = sheet.getRow(i);
                value.setYearMonth(row.getCell(0).toString());
                value.setMonth(row.getCell(1).toString());
                value.setSstAnomalyIndex(Double.parseDouble(row.getCell(2).toString()));
                value.setTemperatureAnomolies(Double.parseDouble(row.getCell(3).toString()));
                value.setPrecipitationAnomolies(Double.parseDouble(row.getCell(4).toString()));
                value.setVaiAnomolies(Double.parseDouble(row.getCell(5).toString()));
                Value.add(value);
            }
            return Value;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private List<AorestCoverageVO> saveExcelAorest(MultipartFile file) {

        try {
            List<AorestCoverageVO> Value = new ArrayList<>();
            InputStream inputStream = file.getInputStream();
            Workbook book;
            Sheet sheet;
            book = new XSSFWorkbook(inputStream);
            sheet = book.getSheetAt(0);
            for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
                AorestCoverageVO value = new AorestCoverageVO();
                Row row = sheet.getRow(i);
                value.setRegion(row.getCell(0).toString());
                value.setRegionEn(row.getCell(1).toString());
                value.setProtectedLands(Double.parseDouble(row.getCell(2).toString()));
                value.setNonProtectedLands(Double.parseDouble(row.getCell(3).toString()));
                value.setMountain(Double.parseDouble(row.getCell(4).toString()));
                value.setNonMountain(Double.parseDouble(row.getCell(5).toString()));
                value.setCreateTime(LocalDateTime.now());
                Value.add(value);
            }
            return Value;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private List<VegetationCoverageVO> saveExcelnpp(MultipartFile file) {

        try {
            List<VegetationCoverageVO> Value = new ArrayList<>();
            InputStream inputStream = file.getInputStream();
            Workbook book;
            Sheet sheet;
            book = new XSSFWorkbook(inputStream);
            sheet = book.getSheetAt(0);
            for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
                VegetationCoverageVO value = new VegetationCoverageVO();
                Row row = sheet.getRow(i);
                value.setRegion(row.getCell(0).toString());
                value.setRegionEn(row.getCell(1).toString());
                value.setProtectedLands(Double.parseDouble(row.getCell(4).toString()));
                value.setNonProtectedLands(Double.parseDouble(row.getCell(5).toString()));
                value.setMountain(Double.parseDouble(row.getCell(2).toString()));
                value.setNonMountain(Double.parseDouble(row.getCell(3).toString()));
                value.setCreateTime(LocalDateTime.now());
                Value.add(value);
            }
            return Value;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    private List<RegionalSystemVO> saveExcelRegion(MultipartFile file) {

        try {
            List<RegionalSystemVO> Value = new ArrayList<>();
            InputStream inputStream = file.getInputStream();
            Workbook book;
            Sheet sheet;
            book = new XSSFWorkbook(inputStream);
            sheet = book.getSheetAt(0);
            for (int i = 1; i < sheet.getLastRowNum() + 1; i++) {
                RegionalSystemVO value = new RegionalSystemVO();
                Row row = sheet.getRow(i);
                value.setRegion(row.getCell(0).toString());
                value.setRegionEn(row.getCell(1).toString());
                value.setTypeConversion(row.getCell(2).toString());
                value.setTypeconversionEn(row.getCell(3).toString());
                value.setProportionOfChangedAreas(Double.parseDouble(row.getCell(4).toString()));
                value.setCreateTime(LocalDateTime.now());
                Value.add(value);
            }
            return Value;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
